package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"io"
	"os"
)

func main() {
	router := gin.Default()

	router.POST("/upload", func(context *gin.Context) {
		file, err := context.FormFile("file")
		if err != nil {
			fmt.Println(err)
		}
		fmt.Println(file.Filename)
		fmt.Println(file.Size / 1024)
		readerFile, err := file.Open()
		file1, err := io.ReadAll(readerFile)
		fmt.Println(file1)
		writerFile, _ := os.Create("./ uploads/13.png")
		defer writerFile.Close()
		n, _ := io.Copy(writerFile, readerFile)
		fmt.Println(n)

		context.JSON(200, gin.H{"msg": "上传成功"})
	})

	router.POST("/upload2", func(context *gin.Context) {
		form, _ := context.MultipartForm()
		files := form.File["upload[]"]
		for _, file := range files {
			err := context.SaveUploadedFile(file, "./uploads/ "+file.Filename)
			if err != nil {
				return
			}
		}
		context.JSON(200, gin.H{
			"msg": fmt.Sprintf("成功上传%d个文件", len(files)),
		})

		context.JSON(200, gin.H{"msg": "上传成功"})
	})

	router.Run(":8080")
}
